var wait = 60;//短信时间
var _false = false;
$(function () {
    $(".register").click(function () {
        register();//点击注册
    })
    $('.code').bind("click",function () {
        if(!_false){
            SendCode(); //发送验证码
        }
    });
})

//点击注册
function register() {
if($(".userName").val()==""){
    YDUI.dialog.toast('用户姓名不能为空','none',function(){});
    return;
}else if($(".pwd").val()==""){
    YDUI.dialog.toast('密码不能为空','none',function(){});
    return;
}else if($(".pwd").val().length > 16 || $(".pwd").val().length < 6){
    YDUI.dialog.toast('密码不符合规范，请重新输入','none',function(){});
    return;
}else if($(".pwd-btn").val() != $(".pwd").val()){
    YDUI.dialog.toast('确认密码错误，请重新输入','none',function(){});
    return;
}else if($(".phone").val()==""){
    YDUI.dialog.toast('手机号码不能为空','none',function(){});
    return;
}else if($(".phone").val().length != 11){
    YDUI.dialog.toast('手机号码有错误，请重新输入','none',function(){});
    return;
}else if($(".code-auth").val()==""){
    YDUI.dialog.toast('验证码不能为空','none',function(){});
    return;
}else if($(".age").val()==""){
    YDUI.dialog.toast('年龄不能为空','none',function(){});
    return;
}else if($('#read').is(':checked') == false){
    YDUI.dialog.toast('您还没有勾选协议','none',function(){});
    return;
} else {
    uerRegister();//用户注册
}
}
//用户注册
var sex = '';
function uerRegister() {
    if($("#sex1").is(":checked") == false && $("#sex2").is(":checked") == false){
        YDUI.dialog.toast('您还没有选择性别','none',function(){});
    }else {
        if($("#sex1").is(":checked") == true){
            sex = '男';
        }else if($("#sex2").is(":checked") == true){
            sex = '女';
        }
        $.ajax({
            type: "get",
            cache:true,
            url:"http://115.159.106.174:8080/vanoo/weixin/worker_add.do?fwkrname="+$(".userName").val()+"&fphone="+$(".phone").val()+"&wxpwd="+$(".pwd").val()+"&fwkrsex="+ sex +"&fbirthday="+$(".age").val()+"&token=123",
            data: '',
            dataType: "json",
            beforeSend:function () {
            },
            complete:function () {
            },
            success: function(result){
                if(result.msg == 'success'){
                    YDUI.dialog.toast('注册成功','',function(){});
                    //console.log(result);
                    setTimeout(function () {
                        window.location.href = 'login.html';//注册成功跳转到登录页面
                    },1000)
                }else {
                    YDUI.dialog.toast(result.msg,'error',function(){});
                }

            },error : function(e) {
                YDUI.dialog.toast(e.msg,'error',function(){});
            }
        });
    }

}

//发送验证码
function SendCode() {
    if ($(".phone").val() == "") {
        YDUI.dialog.toast('请先输入手机号码','none',function(){});
    } else if ($(".phone").val().length != 11) {
        YDUI.dialog.toast('手机号码格式不正确','none',function(){});
    } else {
        $.ajax({
            type: "get",
            cache:true,
            url:"http://115.159.106.174:8080/vanoo/weixin/phonecode.do?phone="+$('.phone').val()+"&token=123",
            data: '',
            dataType: "json",
            beforeSend:function () {
            },
            complete:function () {
            },
            success: function(result){
                if(result.msg == "success"){
                    YDUI.dialog.toast('发送成功','success',function(){});
                    SendTime(".code");
                }else {
                    YDUI.dialog.toast(result.msg ,'error',function(){});
                }

            },error : function(e) {
                YDUI.dialog.toast(e.msg,'error',function(){});
            }
        });

    }
}

function SendTime(parameter){
    var Send = $(""+parameter+"");
    if (wait == 0) {
        Send.val("发送验证码");
        wait = 60;
        _false  = false;
    } else {
        Send.val("重新发送(" + wait + ")s");
        wait--;
        _false = true;
        setTimeout(function() {
            SendTime(parameter);
        },1000);
    }
}
